using System;
using System.Collections.Generic;
using System.Linq;

using Csla;

using EntModApp.EntServices.Business;
using EntModApp.EntServices.IoC;

using DemoApp.DataAccess;

namespace DemoApp.BusinessLayer 
{
  [Serializable]
  public partial class CslaGroupEdit : 
    CslaUserEdit,
    IGroup
  {
    #region [ Business Properties ]

    public static PropertyInfo<CslaUserEditCollection> UsersPropertyInfo = RegisterProperty(new PropertyInfo<CslaUserEditCollection>("Users"));
    public CslaUserEditCollection Users 
    { 
      get
      {
        return GetProperty(UsersPropertyInfo);
      }
      private set
      {
        LoadProperty(UsersPropertyInfo, value);
      }
    }

    #endregion [ Business Properties ]

    #region [ Validation Rules ]
    #endregion [ Validation Rules ]

    #region [ Authorization Rules ]
    #endregion [ Authorization Rules ]
	
    #region [ Factory Methods ]

    #region [ Root Factory Methods ]
    public static CslaGroupEdit New()
    {
      return DataPortal.Create<CslaGroupEdit>();
    }

    public static void Delete(UserKey key)
    {
      DataPortal.Delete<Group>(new SingleCriteria<Group, UserKey>(key));
    }

    public static void Delete(Guid UserId)
    {
      Delete(new UserKey { UserId = UserId });
    }
    #endregion [ Root Factory Methods ]

    #region [ Child Factory Methods ]
    internal static CslaGroupEdit NewChild()
    {
      return DataPortal.CreateChild<CslaGroupEdit>();
    }

    internal static CslaGroupEdit GetGroupEdit(Group data, GroupLoadSettings loadSettings)
    {
      if (data == null) throw new ArgumentNullException("data");
      if (loadSettings == null) throw new ArgumentNullException("loadSettings");
      CslaGroupEdit result = new CslaGroupEdit();
      result.FetchGroup(data, loadSettings);
      result.MarkAsChild();
      return result;
    }
    #endregion [ Child Factory Methods ]

    private static int ForceInitField = 0;
    protected CslaGroupEdit() 
    {
      ForceInitField = 1;
    }
    #endregion [ Factory Methods ]

    #region [ Data Access ]

    #region [ Root Data Access ]
    protected override void DataPortal_Create()
    {
      base.DataPortal_Create();
    }

    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Insert()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Update()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_DeleteSelf()
    {
      DataPortal_Delete(new SingleCriteria<Group, UserKey>(new UserKey { UserId = this.UserId }));
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    private void DataPortal_Delete(SingleCriteria<Group, UserKey> criteria)
    {
    }
    #endregion [ Root Data Access ]

    #region [ Child Data Access ]
    protected override void Child_Create()
    {
      base.Child_Create();
    }
    
    private void Child_Fetch(object childData)
    {
    }
    
    private void Child_Insert(object parent)
    {
    }
    
    private void Child_Update(object parent)
    {
    }
    
    private void Child_DeleteSelf(object parent)
    {
    }
    #endregion [ Child Data Access ]

    protected void FetchGroup(Group data, GroupLoadSettings loadSettings)
    {
      FetchGroupData(data, loadSettings);
      FetchGroupChildren(data, loadSettings);
    }

    protected void FetchGroupData(Group data, GroupLoadSettings loadSettings)
    {
      base.FetchUserData(data, loadSettings);
    }

    protected void FetchGroupChildren(Group data, GroupLoadSettings loadSettings)
    {
      base.FetchUserData(data, loadSettings);
      if (loadSettings.Inclusion.IncludeUsers)
        this.Users = CslaUserEditCollection.GetUserEditCollection(data.Users, loadSettings.Inclusion.IncludedUsersSettings);
    }
    #endregion [ Data Access ]

    #region [ Explicit IGroup implementation ]

    IBusinessObjectCollection<IUser> IGroup.Users
    { 
      get
      {
        return this.Users;
      }
    }


    bool IBusinessObject.IsReadonly
    {
      get { return false; }
    }

    bool IBusinessObject.IsDirty
    {
      get { return this.IsDirty; }
    }

    #endregion [ Explicit IGroup implementation ]
  }
}
